; sprites.asm
; Moving objects patterns

	.include "global.inc"
	.include "frame.inc"
	
	_IN_SPRITES = 1
	.include "sprites.inc"
	
		SHAPES_SIZE_2x2 = 32
		SHAPES_BLOCK_SIZE_2x2 = 9*SHAPES_SIZE_2x2

	.segment "CODE"

	shapesrc = tmpptr2
	
	sprite1 = tmpptr1 
	sprite2 = tmpptr2 
	sprite3 = tmpptr3 
	sprite4 = tmpptr4 
	
initspr:	; init shapes 2x2
			ld16 sprite1, shapes2x2		; setup source ptr

			ldx #0						; start with first shape
			stx shapeno
@shplp:		lda shapelo, x				; setup shape ptr
			sta shapesrc
			lda shapehi, x
			sta shapesrc+1
			
			ldy #6						; clr leading 7 bytes
			lda #0
@clrprf:	sta (sprite1), y
			dey
			bpl @clrprf
			mov16 sprite1, 7
			
			lda sprite1					; save start address
			sta sh2x2ptrlo, x
			lda sprite1+1
			sta sh2x2ptrhi, x
			
			ldy #31						; copy shape
@cplp:		lda (shapesrc), y
			sta (sprite1), y
			dey
			bpl @cplp

			mva sprite1, sprite2, 16	; setup ptrs
			mva sprite2, sprite3, 16
			mva sprite3, sprite4, 16
			
			ldx #0
@rorspr:	ldy #15						; (sprite_n+2) := (sprite_n) >> 1
@ror:		lda (sprite1), y
			lsr
			sta (sprite3), y
			lda (sprite2), y
			ror
			sta (sprite4), y
			dey
			bpl @ror
			cpx #6
			beq @next
			inx
			mov16 sprite1, 32 
			mov16 sprite2, 32 
			mov16 sprite3, 32 
			mov16 sprite4, 32
			jmp @rorspr  
			
@next:		mov16 sprite1, 64 
			ldx shapeno					; next shape
			inx
			cpx #NUM_SHAPES_2x2
			beq @fin2x2
			stx shapeno
			jmp @shplp
			
@fin2x2:	ldx #MAX_OBJECTS_2x2-1		; clr states
			lda #0
@ilp:		sta spr2x2act, x
			sta spr1x1act, x
	 		dex
	 		bpl @ilp
			rts
			
calcspr:	; setup frame object for sprite(s)
			ldx #MAX_OBJECTS_2x2-1		; check 2x2 sprites
@chk2x2:	lda spr2x2act, x
			sta active2x2, x
			beq @skip2x2
			
			clc							; calc screen offset
			lda spr2x2row, x
			tay
			lda rowofflo, y
			adc spr2x2col, x
			sta poslo2x2, x
			lda rowoffhi, y
			adc #0
			sta poshi2x2, x
			lda spr2x2clr, x			; copy color
			sta color2x2, x
			
			lda spr2x2idx, x			; set row offset
			tay
			sec
			lda sh2x2ptrlo, y
			sbc spr2x2piy, x
			sta datalo2x2, x
			lda sh2x2ptrhi, y
			sbc #0
			sta datahi2x2, x
			
			lda spr2x2pix, x			; set column offset
			tay
			clc
			lda datalo2x2, x
			adc mul32t, y
			sta datalo2x2, x
			bcc @skip2x2
			inc datahi2x2, x
			
@skip2x2:	dex
			bpl @chk2x2			

			ldx #MAX_OBJECTS_1x1-1		; check 1x1 sprites

@chk1x1:	lda spr1x1act, x
			sta active1x1, x
			beq @skip1x1
			
			clc							; calc screen offset
			lda spr1x1row, x
			tay
			lda rowofflo, y
			adc spr1x1col, x
			sta poslo1x1, x
			lda rowoffhi, y
			adc #0
			sta poshi1x1, x
			lda spr1x1clr, x			; copy color
			sta color1x1, x
			
			lda spr1x1pix, x			; set column & row offset
			tay 
			sec
			lda sh1x1ptrlo, y
			sbc spr1x1piy, x
			sta datalo1x1, x
			lda sh1x1ptrhi, y
			sbc #0
			sta datahi1x1, x
			
@skip1x1:	dex
			bpl @chk1x1			

			
			rts

	.segment "DATA"
	
shapeno:	.byte 0

	.segment "RODATA"

mul32t:		.repeat 8, I
				.byte 32*I
			.endrep 

mul8t:		.repeat 8, I
				.byte 8*I
			.endrep 

rowofflo:	.repeat ROWS, I
				.byte <(I*COLS)
			.endrep	
rowoffhi:	.repeat ROWS, I
				.byte >(I*COLS)
			.endrep	

shapelo:	.byte <updn1, <updn2, <rght1, <rght2, <left1, <left2, <dyin1, <dyin2, <idle 
shapehi:	.byte >updn1, >updn2, >rght1, >rght2, >left1, >left2, >dyin1, >dyin2, >idle 
	
updn1:		.byte $08, $1C, $08, $3F, $49, $5C, $1C, $17
			.byte $70, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00

updn2:		.byte $08, $1C, $08, $7E, $49, $1D, $1C, $74
			.byte $07, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00

rght1:		.byte $08, $1C, $08, $1F, $28, $48, $14, $22
			.byte $73, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00
			.byte $80, $00, $00, $00, $00, $00, $00, $00
			
rght2:		.byte $08, $1C, $08, $78, $0E, $09, $78, $48
			.byte $4E, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00
			
left1:		.byte $08, $1C, $08, $7C, $0A, $08, $14, $22
			.byte $E7, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00

left2:		.byte $08, $1C, $08, $0F, $38, $48, $0F, $09
			.byte $39, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00

dyin1:		.byte $08, $1C, $49, $3E, $08, $08, $49, $55
			.byte $22, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00

dyin2:		.byte $00, $00, $00, $08, $2A, $3E, $88, $9C
			.byte $63, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $80, $80
			.byte $00, $00, $00, $00, $00, $00, $00, $00

idle:		.byte $08, $1C, $08, $7F, $49, $5D, $1C, $14
			.byte $77, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00
			.byte $00, $00, $00, $00, $00, $00, $00, $00


shapes1x1:	.repeat 7
				.byte 0
			.endrep
			.repeat 8, I
				.byte $80 >> I
				.repeat 7
					.byte 0
				.endrep
			.endrep

sh1x1ptrlo:	.repeat 8, I
				.byte <(shapes1x1 + 7 + 8*I)
			.endrep	
sh1x1ptrhi:	.repeat 8, I
				.byte >(shapes1x1 + 7 + 8*I)
			.endrep
   
	.segment "BSS"

spr2x2act:	.res MAX_OBJECTS_2x2
spr2x2col:	.res MAX_OBJECTS_2x2
spr2x2row:	.res MAX_OBJECTS_2x2
spr2x2pix:	.res MAX_OBJECTS_2x2
spr2x2piy:	.res MAX_OBJECTS_2x2
spr2x2idx:	.res MAX_OBJECTS_2x2
spr2x2clr:	.res MAX_OBJECTS_2x2
spr2x2coll = coll2x2

spr1x1act:	.res MAX_OBJECTS_1x1
spr1x1col:	.res MAX_OBJECTS_1x1
spr1x1row:	.res MAX_OBJECTS_1x1
spr1x1pix:	.res MAX_OBJECTS_1x1
spr1x1piy:	.res MAX_OBJECTS_1x1
spr1x1clr:	.res MAX_OBJECTS_1x1
spr1x1coll = coll1x1

shapes2x2:	.res (7+8*32)*NUM_SHAPES_2x2

sh2x2ptrlo:	.res NUM_SHAPES_2x2
sh2x2ptrhi:	.res NUM_SHAPES_2x2
